package com.zretc.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.print.attribute.standard.MediaSize.Other;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.entity.Star;
import com.zretc.service.StarService;
import com.zretc.service.impl.StarServiceImpl;

/**
 * Servlet implementation class StarServlet
 */
@WebServlet("/star.do")
public class StarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	StarService starService = new StarServiceImpl();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置请求(post)编码，获取请求参数的值
		request.setCharacterEncoding("utf-8");
		//设置响应的数据类型:MIME
		response.setContentType("application/json;charset=utf-8");
		// 获取参数
		String nickName = request.getParameter("nickName");
		// 输出验证
		System.out.println("nickName :" + nickName);

		// 调用service的方法，返回结果:
		Star star = starService.getStar(nickName);

		// 将 star数据返回给前台；淡然更合理的方法是将star封装起来再返回,类似之前ajaxResposne
		// 今天暂时只用返回star对象的方法

		PrintWriter out = response.getWriter();

		Gson gson = new Gson();

		String jsonString = gson.toJson(star);
		out.print(jsonString);

		out.close();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
